﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using HKEA.CMS.Core.BLL.Mtbl;


namespace WebApplication.Ashx
{
    /// <summary>
    /// GetProductCategory 的摘要说明
    /// </summary>
    public class GetProductCategory : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            Object obj = new Object();
            lock (obj)
            {
                context.Response.ContentType = "text/plain";
                int _ProductCatUID = Convert.ToInt32(context.Request["ProductCatUID"]);
                mtblProductSubCat ent = new mtblProductSubCat();
                mtblProductSubCatBLL bll = mtblProductSubCatBLL.GetInstance();
                ent.ProductCatUID = _ProductCatUID;
                List<mtblProductSubCat> list = null;
                StringBuilder data = new StringBuilder();
                string tmp = string.Empty;
                data.Append("[");
                list = bll.GetListByENT(ent);
                string strFormat = "{{ProductSubUid:'{2}',ProductSubCatEng:'{0}',ProductSubCatChi:'{1}'}}";
                int i = 0;
                foreach (mtblProductSubCat item in list)
                {
                    tmp = string.Format(strFormat, DealString(item.ProductSubCat_Eng.ToString()), DealString(item.ProductSubCat_Chi.ToString()),DealString(item.ProductSubCatUID.ToString())); // "{'ProductSubCatEng':'" + item.ProductSubCat_Eng + "','ProductSubCatChi':'"+ item.ProductSubCat_Chi + "'},";
                    data.Append(tmp);
                    if (i < list.Count - 1)
                    {
                        data.Append(",");
                    }
                    i++;
                }
                data.Append("]");
                context.Response.Write(data.ToString());
            }
        }

        /// <summary>
        /// 处理转义字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string DealString(string str)
        {
            return HttpContext.Current.Server.HtmlEncode(Regex.Replace(str, @"[\b\\\f\r\t\n\v]", "").Replace("\'", "\\\'"));
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}